package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.sleepcare.BindSleepCareDto;
import com.csun.cmny.provider.model.dto.sleepcare.SleepCarePageQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.DmcSleepCareVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcSleepCareBindService;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:12
 * @Description: the class dmc device sleep care controller.
 */
@RestController
@RequestMapping(value = "/sleepCare", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcSleepCareController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcSleepCareBindService dmcSleepCareBindService;

    /**
     * 查询组织智能床垫设备列表
     *
     * @param sleepCarePageQuery   the sleep care page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询组织智能床垫设备列表")
    @RequestMapping(value = "/queryGroupSleepCareListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupSleepCareListWithPage(@RequestBody SleepCarePageQuery sleepCarePageQuery) {

        sleepCarePageQuery.setGroupId(getLoginAuthDto().getGroupId());
        sleepCarePageQuery.setDeviceType(DmcDeviceTypeEnum.BS_SLEEP.getIndex());

        PageInfo pageInfo = dmcSleepCareBindService.queryGroupSleepCareListWithPage(sleepCarePageQuery);

        for (Object o : pageInfo.getList()) {
            DmcSleepCareVo dmcSleepCareVo = (DmcSleepCareVo) o;
            if (!PubUtils.isNull(dmcSleepCareVo.getBindType())) {
                dmcSleepCareVo.setBind(true);
                switch (dmcSleepCareVo.getBindType()) {
                    case DmcConstant.SLEEP_CARE_BIND_TYPE_OLDMAN :
                        OldmanBedRoomBuildingVo obv = (OldmanBedRoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcOldmanId(dmcSleepCareVo.getOldmanId()));
                        replaceOldmanInfo(dmcSleepCareVo, obv);
                        break;
                    case DmcConstant.SLEEP_CARE_BIND_TYPE_BED :
                        OldmanBedRoomBuildingVo bov = (OldmanBedRoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcBedId(dmcSleepCareVo.getBedId()));
                        replaceOldmanInfo(dmcSleepCareVo, bov);
                        break;
                }
            }
        }

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    public void replaceOldmanInfo(DmcSleepCareVo dmcSleepCareVo, OldmanBedRoomBuildingVo bov) {
        if (PublicUtil.isNotEmpty(bov)) {
            if (PublicUtil.isNotEmpty(bov.getBedId())) {
                /**
                 * @date 2019-05-06
                 * @author chisj
                 * @desc 解决老人换床后，依旧显示之前老人信息的bug #1832
                 */
                // 绑定床位, 老人信息和位置根据床位获取
                dmcSleepCareVo.setOldmanName(bov.getOldmanName());
                /**
                 * @date 2019-05-28
                 * @author chisj
                 * @desc 显示【房间-床位】 #2812
                 */
                if (PublicUtil.isNotEmpty(bov.getRoomNo()) && PublicUtil.isNotEmpty(bov.getBedNo())) {
                    dmcSleepCareVo.setLocation(bov.getRoomNo() + "-" + bov.getBedNo());
                } else {
                    dmcSleepCareVo.setLocation("--");
                }
            }
        }
    }

    /**
     * 添加智能床垫设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加智能床垫设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addSleepCare(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.BS_SLEEP.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改智能床垫设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改智能床垫设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editSleepCare(@Validated(value = {DmcDeviceEditValidator.class})
                                 @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.BS_SLEEP.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除睡眠卫士
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除日创床垫")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcSleepCareById(@PathVariable Long id) {

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.failed",null));
        }

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 绑定睡眠卫士
     *
     * @param bindSleepCareDto bind sleep care dto
     *
     * @return the wrapper.
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/bindSleepCare", method = RequestMethod.POST)
    public Wrapper bindSleepCare(@Validated @RequestBody BindSleepCareDto bindSleepCareDto,
                                 BindingResult br) {

        Integer result = dmcSleepCareBindService.bindSleepCare(bindSleepCareDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑睡眠卫士
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/breakSleepCare/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakSleepCare(@PathVariable Long deviceId) {


        Integer result = dmcSleepCareBindService.breakSleepCare(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
